package com.example.library_common.utils.upload_file

import android.text.TextUtils
import android.util.Log
import android.widget.Toast
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.JSONObject
import com.example.library_common.R
import com.example.library_common.bean.CommonBean
import com.example.library_common.db.UserManager
import com.example.library_common.global.AppGlobals
import com.example.library_common.networking.RestClient
import ikidou.reflect.TypeBuilder
import top.zibin.luban.Luban
import top.zibin.luban.OnCompressListener
import java.io.File

class UploadFileWorker {


    private var apiToken = UserManager.getInstance().user.api_token

    private var mData = mutableListOf<String>()

    private var indexOf = 0

//    private var ls = mutableListOf<String>()

    private var res = mutableListOf<String>()

    private var mListener: UpLoadOverListener? = null

    fun setListener(mListener: UpLoadOverListener?): UploadFileWorker {
        this.mListener = mListener
        return this
    }

    fun doWork(ls: List<String>) {
        mData.clear()
        mData.addAll(ls)
        if (mData.size <= 0) {
            mListener?.onUpLoadOver(res)
            return
        }
        compress()
    }

    private fun compress() {
        val fileCus = mData[indexOf]
        Luban.with(AppGlobals.getApplication())
            .load(fileCus)
            .setCompressListener(object : OnCompressListener {
                override fun onSuccess(file: File?) {
                    uploadFile(file)
                }

                override fun onError(e: Throwable?) {
                    uploadFile(File(fileCus))
                }

                override fun onStart() {

                }

            }).launch()
    }

    private fun uploadFile(file: File?) {
        RestClient.builder()
            .url(R.string.uploadFile)
//            .params("api_token", apiToken)
            .file(file)
            .success {
                Log.d("uploadFile", "uploadFile_data: $it")
                parseData(it)

                nextOne()

            }.failure {
                Toast.makeText(AppGlobals.getApplication(), "发送失败", Toast.LENGTH_LONG).show()
                nextOne()
            }
            .error { _, msg ->
                Toast.makeText(AppGlobals.getApplication(), msg, Toast.LENGTH_LONG).show()
                nextOne()
            }.build()
            .upload()
    }

    private fun nextOne() {

        indexOf++

        if (indexOf < mData.size) {

            compress()
        } else {
            mListener?.onUpLoadOver(res)
        }
    }


    private fun parseData(it: String?) {

        val bean:UploadFileBean = JSON.parseObject(it, UploadFileBean::class.java)

        val filePath = bean.filePath
        if (TextUtils.isEmpty(filePath)) {
            return
        }

        res.add(filePath)

//        val parse = JSON.parse(bean.data)
////        Log.d("pic_id", "bean.data: ${bean.data}")
//        if (parse is JSONObject) {
////            Log.d("pic_id", "pic_id: ${parse.getString("pic_id")}")
//            res.add(parse.getString("pic_id"))
//
//        }
    }

    interface UpLoadOverListener {
        fun onUpLoadOver(res: List<String>)
    }
}